<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */
 
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Shoufei as ShoufeiModel;
use app\admin\validate\ShoufeiValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Shoufei extends BaseController

{
	/**
     * 构造函数
     */
	protected $uid;
	protected $model;
    public function __construct()
    {
        $this->model = new ShoufeiModel();
		$this->uid = get_login_admin('id');
    }
    /**
    * 数据列表
    */
    public function datalist()
    {
        if (request()->isAjax()) {
			$param = get_params();

			$where = [];
			if(!empty($param['keywords'])){
				$where[] = ['s.name', 'like', '%' . $param['keywords'] . '%'];
			}
			$where['a.is_delete']=1;
			//$where['a.chongzhi']=1;
            $list = $this->model->getShoufeiList($where,$param);
            //pr($list);
            foreach($list as $key=>$val){
				/*$nianji= Db::name('nianji')->where(['id'=>$val['nianji'],])->find();
				$nianji_log[$key]['nianji'] =$nianji['nianji'];*/

				/*$student= Db::name('student')->where(['id'=>$val['student_id'],])->find();
				$list[$key]['student_id'] =$student['name'];*/

				$type= Db::name('type')->where(['id'=>$val['type_id'],])->find();
				$list[$key]['type_id'] =$type['title'];
				if($val['shichang_id']==0){
					$list[$key]['shichang_id'] ='无';
				}else{
					$type= Db::name('shichang')->where(['id'=>$val['shichang_id'],])->find();
					$list[$key]['shichang_id'] =$type['shichang'];
				}
				

				$list[$key]['jiaofei_time']=date('Y-m-d',$val['jiaofei_time']);
            	$list[$key]['start_time']=date('Y-m-d',$val['start_time']);
            	$list[$key]['end_time']=date('Y-m-d',$val['end_time']);
            	if(!empty($val['baohan'])){
            		$baohan = explode(',',$val['baohan']);
            		$baohanCount = count($baohan);
            		$type='';
            		$type2='';
            		foreach($baohan as $key2 =>$val2){
            			if($key2+1 ==$baohanCount){
            				$type .= Db::name('type')->field('title')->where(['id'=>$val2])->find()['title'];
            			}else{
            				$type .= Db::name('type')->field('title')->where(['id'=>$val2])->find()['title'].',';
            			}
            			
            			/*$type = Db::name('type')->field('title')->where(['id'=>$val2])->find();
            			$type2.=$type2.$type['title']." ";*/
            			
						
            		}
            		
            		$list[$key]['baohan'] = explode(',',$type);
            		//pr($type,1);
            		
            	}
            	
			}
            return table_assign(0, '', $list);
        }
        else{
            return view();
        }
    }

    /**
    * 添加
    */
    public function add()
    {
        if (request()->isAjax()) {		
			$param = get_params();	
			$baohanData = isset($param['baohan']) ? $param['baohan'] : 0;
            $param['baohan'] = implode(',', $baohanData);
            $param['chongzhi'] = 1;
            $param['create_admin'] =$this->uid;
			//pr($param);
            // 检验完整性
            try {
                validate(ShoufeiValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
			if(isset($param["jiaofei_time"])){
					$param["jiaofei_time"]= $param["jiaofei_time"]?strtotime($param["jiaofei_time"]):0;
				}
				if(isset($param["start_time"])){
					$param["start_time"]= $param["start_time"]?strtotime($param["start_time"]):0;
				}
				if(isset($param["end_time"])){
					$param["end_time"]= $param["end_time"]?strtotime($param["end_time"]):0;
				}
				
            $this->model->addShoufei($param);
        }else{
			return view();
		}
    }
	

    /**
    * 编辑
    */
    public function edit()
    {
		$param = get_params();
		
        if (request()->isAjax()) {	
        	$baohanData = isset($param['baohan']) ? $param['baohan'] : 0;
            $param['baohan'] = implode(',', $baohanData);		
            // 检验完整性
            try {
                validate(ShoufeiValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
			if(isset($param["jiaofei_time"])){
					$param["jiaofei_time"]= $param["jiaofei_time"]?strtotime($param["jiaofei_time"]):0;
				}
				if(isset($param["start_time"])){
					$param["start_time"]= $param["start_time"]?strtotime($param["start_time"]):0;
				}
				if(isset($param["end_time"])){
					$param["end_time"]= $param["end_time"]?strtotime($param["end_time"]):0;
				}
				
            $this->model->editShoufei($param);
        }else{
			$id = isset($param['id']) ? $param['id'] : 0;
			$detail = $this->model->getShoufeiById($id);
			if (!empty($detail)) {
				if(!empty($detail['baohan'])){
					$detail['baohan'] = explode(',', $detail['baohan']);
				}
				View::assign('detail', $detail);
				return view();
			}
			else{
				throw new \think\exception\HttpException(404, '找不到页面');
			}			
		}
    }
    /**
    * 续费
    */
    public function xufei()
    {
		$param = get_params();
		
        if (request()->isAjax()) {	
        	$baohanData = isset($param['baohan']) ? $param['baohan'] : 0;
            $param['baohan'] = implode(',', $baohanData);	
            $param['chongzhi'] = 2;	
            // 检验完整性
            try {
                validate(ShoufeiValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
			if(isset($param["jiaofei_time"])){
					$param["jiaofei_time"]= $param["jiaofei_time"]?strtotime($param["jiaofei_time"]):0;
				}
				if(isset($param["start_time"])){
					$param["start_time"]= $param["start_time"]?strtotime($param["start_time"]):0;
				}
				if(isset($param["end_time"])){
					$param["end_time"]= $param["end_time"]?strtotime($param["end_time"]):0;
				}
				
            $this->model->addShoufei($param);
        }else{
			$id = isset($param['id']) ? $param['id'] : 0;
			$detail = $this->model->getShoufeiById($id);
			if (!empty($detail)) {
				if(!empty($detail['baohan'])){
					$detail['baohan'] = explode(',', $detail['baohan']);
				}
				View::assign('detail', $detail);
				return view();
			}
			else{
				throw new \think\exception\HttpException(404, '找不到页面');
			}			
		}
    }

    /**
    * 查看信息
    */
    public function read()
    {
        $param = get_params();
		$id = isset($param['id']) ? $param['id'] : 0;
		$detail = $this->model->getShoufeiById($id);
		if (!empty($detail)) {
			View::assign('detail', $detail);
			return view();
		}
		else{
			throw new \think\exception\HttpException(404, '找不到页面');
		}
    }

    /**
    * 删除
	* type=0,逻辑删除，默认
	* type=1,物理删除
    */
    public function del()
    {
        $param = get_params();
		$id = isset($param['id']) ? $param['id'] : 0;
		$type = isset($param['type']) ? $param['type'] : 0;

        $this->model->delShoufeiById($id,$type);
   }
}
